<?php

namespace Main\Api\App;

class post extends common
{
    public function disuss()
    {
	
        //跨域提交
        header('Access-Control-Allow-Origin:http://' . HOST_DOMAIN . ',https://' . HOST_DOMAIN);

        header('Access-Control-Allow-Methods:POST,GET');

        header('Access-Control-Allow-Credentials:true');

        header('Access-Control-Max-Age: 1000');

        $post = \FC\Input($_POST);
		
		$referer = $post['nowurl'] ?? '';

		$re_url = parse_url($referer);
		
		$path = $re_url['path']??'';
		
		if(empty($path)){
		    $this->error(1, '提交失败');
		}

        $times = $this->SESSION->get('times'); //获取时间

        if ($times && (TIME - $times < 30)) {
            $this->error(1, '好快啊，系统小姐姐吃不消了！');
        }

        $hf_uid = $post['hf_uid'] ?? 0;

        $page_name = $path;

        $content = $this->comments_model->checkInput('content', $post['comment']);
        
		//$content = $post['comment'] ?? '';
        if (empty($content)) {
            $this->error(1, '评论内容不能为空！');
        }

        if ($hf_uid && $hf_uid!=0) {
            if (!$this->commentator_model->getOnly(['uid'=>$hf_uid])) {
                $this->error(1, '回复用户并不存在！');
            }
        }
		
        $nick = $this->commentator_model->checkInput('nickname', $post['author']);
        if (!$nick) {
            $this->error(1, '昵称不能为空,太长或含有特殊字符！');
        }		
		
        $email = $post['email'] ?? '';
		
        $email = $this->commentator_model->checkInput('email', $email);
		
		if(empty($email)){
			$this->error(1, '邮箱格式错误');
		}
	
        $imgurl= $this->commentator_model->get_avatar_url();
		
        $homeurl = $post['homeurl'] ?? '';
        
        $homeurl = !empty($homeurl) ? $this->commentator_model->checkInput('homeurl', $homeurl) : '';
	
        $only = $this->commentator_model->getOnly(['email'=>$email],'uid,status');
		
		$uid = $only['uid'] ?? 0;
		
		$status = $only['status'] ?? 0;
		
        if ($status == 2) {
            $this->error(1, '抱歉,你已被系统拉黑!');
        }
		
        if ($hf_uid && $hf_uid == $uid) {
            $this->error(1, '自己回复自己,戏精吗？');
        }
        if ($uid) {
            $datas = ['email' => $email, 'nickname' => $nick, 'homeurl' => $homeurl];
			$where = ['uid' => $uid];
			$this->commentator_model->checkUpdate($datas, $where);
        } else {
            $datas = ['email' => $email, 'nickname' => $nick, 'imgurl' => $imgurl, 'status'=> 1, 'hfip'=> 1,'cdat' => TIME];
			$uid= $this->commentator_model->checkSave($datas);
        }
        $expire = 31104000;
        $this->COOKIES->expire($expire)->path('/')->set('nick', $nick);
        $this->COOKIES->expire($expire)->path('/')->set('email', $email);
        $this->COOKIES->expire($expire)->path('/')->set('homeurl', $homeurl);
        $this->COOKIES->expire($expire)->path('/')->set('imgurl', $imgurl);
        //写入评论
        $post = ['uid' => $uid, 'page_name' => $page_name, 'hf_uid' => $hf_uid, 'content' => $content, 'cdat' => TIME];
        $result = $this->comments_model->checkSave($post);
        if ($result) {
            $this->SESSION->set('times', TIME);
			$this->sendmail($nick, $hf_uid, $content, $page_name, $referer);
            $this->success('', '评论成功');
        } else {
             $this->error(1, '评论失败');
        }
    }

    //发送邮件
    public function sendmail($name = '', $hf_uid = 0, $text = '', $page_name = '', $url = '')
    {
        if (empty($name) || empty($text) || empty($page_name) || empty($url)) {
            return false;
        }
		$email_config = $this->config;
		
        $email_config['form'] = $email_config['smtp_user'];
        
        $email_config['form_name'] = $email_config['title'];
        
        $email_config['replyto'] = $email_config['smtp_user'];
        
        $email_config['reply_name'] = $email_config['title'];	
		
		if($email_config['email_status'] == 2){
			return false;
		}
		$email_header = $email_config['from_head'];		
		
		$email_content = $email_config['from_template'];
		
        fastcgi_finish_request();
		
        $email = $email_config['smtp_user'];
		
        $hfname = $email_config['title'];		
		
		if($hf_uid!=0){
            $where['uid'] = $hf_uid;
            $info = $this->commentator_model->getOnly($where);
            if (!empty($info)) {
                $email = $info['email'];
                $hfname = $info['nickname'];					
            }
		}
        session_write_close();
        $ubb = array('hfname' => $name, 'name' => $hfname, 'url' => $url, 'text' => $text, 'time' => date('Y-m-d H:i:s'));
        $heading = $this->ubb($ubb, $email_header); //转换标题
        $content = $this->ubb($ubb, $email_content); //转换内容
        $to = $email; //接受邮箱
        $title = $heading;
        $lenght = 80;
        $body = $content;
        $mail = new \PHPMailer\MailSend();
        $mail->config($email_config); //修改配置
        if ($mail->Send($to, $title, $body) == true) {
            return true;
        } else {
            return true;
        }
    }

    // 转换ubb
    public function ubb($list, $content)
    {
        if (!is_array($list)) {
            return $content;
        }
        $x_end_regular = '/{([^}]+?)}/i';
        if (preg_match_all($x_end_regular, $content, $end_arr)) {
            $arr = array_unique($end_arr[0]);
            foreach ($arr as $key => $value) {
                $data = isset($list[$end_arr[1][$key]]) ? $list[$end_arr[1][$key]] : '';
                $content = str_replace($value, $data, $content);
            }
        }
        return $content;
    }
}
